﻿using Authentication.Core.Data;
using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract.Authentication.Record;
using Common.Contract.SystemConfig.Record;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Provider
{
    public class SaleCodeProvider : GenericProvider, ISaleCodeProvider
    {
        public List<DaoCodeS16> RetrieveListSaleCode(out int totalRecord, UserRecord currentUser, FilterRecord filterRecord)
        {
            var userParent = currentUser.BranchId.Substring(currentUser.BranchId.Length - 3);
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.DaoCodeS16s
                        where x.DeptParent.Equals(userParent) || x.Dao.Equals(currentUser.Dao)
                        select x;
            if (!string.IsNullOrEmpty(filterRecord.TextSearchByCif))
            {
                query = query.Where(x => x.Dao.Contains(filterRecord.TextSearchByCif));
            }
            totalRecord = query.Count();
            return query.ToList();
        }

        public List<DaoCodeS16> RetrieveListSaleCodeByDao(List<string> listDao)
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.DaoCodeS16s
                        where listDao.Contains(x.Dao)
                        select x;
            return query.ToList();
        }
        public DaoCodeS16 RetrieveDaoCodeByDao(string dao, UserRecord currentUser)
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.DaoCodeS16s
                        where x.Dao == dao
                        select x;
            return query.FirstOrDefault();
        }
    }
}
